<template>
  <div class="resource-list">
    <el-card class="box-card" shadow="never" :body-style="{ height: '0px', padding: '0px 0px'}" v-permission="24">
      <div slot="header" class="clearfix">
        <el-form ref="form" :inline="true" :model="form" label-width="80px" >
          <div class="filter-container">
            <div>
              <el-form-item prop="name" label="资源名称">
                <el-input v-model="form.name"></el-input>
              </el-form-item>
              <el-form-item prop="url" label="资源路径">
                <el-input v-model="form.url"></el-input>
              </el-form-item>
              <el-form-item prop="categoryId" label="资源分类">
                <el-select
                  v-model="form.categoryId"
                  placeholder="请选择资源分类"
                  clearable
                >
                  <el-option
                    :label="item.name"
                    :value="item.id"
                    v-for="item in resourceCategories"
                    :key="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </div>
            <div>
              <el-form-item>
                <el-button
                  type="primary"
                  @click="onSubmit"
                  :disabled="isLoading"
                >查询搜索</el-button>
                <el-button
                  @click="onReset"
                  :disabled="isLoading"
                >重置</el-button>
              </el-form-item>
            </div>
          </div>
        </el-form>
      </div>
    </el-card>
    <el-card class="box-card" shadow="never" :body-style="{ height: '0px', padding: '0px 0px'}">
      <div slot="header" class="clearfix">
        <el-button
          @click="adResource"
          :disabled="isLoading"
          v-permission="26"
        >添加</el-button>
        <el-button
          @click="$router.push({
            name: 'ResourceCategory'
          })"
          :disabled="isLoading"
          v-permission="20"
        >资源分类</el-button>
      </div>
    </el-card>
    <div class="resource-body">
      <el-table
        :data="resources"
        style="width: 100%; margin-bottom: 20px"
        v-loading="isLoading"
        border
      >
        <el-table-column
          type="index"
          label="编号"
          align="center"
          width="100">
        </el-table-column>
        <el-table-column
          prop="name"
          align="center"
          label="资源名称">
        </el-table-column>
        <el-table-column
          prop="url"
          align="center"
          label="资源路径">
        </el-table-column>
        <el-table-column
          prop="description"
          align="center"
          label="描述">
        </el-table-column>
        <el-table-column
          prop="createdTime"
          align="center"
          label="添加时间">
        </el-table-column>
        <el-table-column
          width="180"
          align="center"
          label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleEdit(scope.row)" v-permission="26">编辑</el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row)" v-permission="28">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :disabled="isLoading"
        :current-page.sync="form.current"
        :page-sizes="[15, 20, 50]"
        :page-size="form.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount">
      </el-pagination>
    </div>
    <el-dialog
      v-if="showDialog"
      :title="!isEdit ? '添加资源' : '编辑资源'"
      :visible.sync="showDialog"
      width="40%"
    >
      <resorce-form
        :isEdit="isEdit"
        :resource="current"
        @on-cancel="showDialog = false"
        @on-submit="onFormSubmit"
      />
    </el-dialog>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getResourcePages, deleteResource } from '@/services/resource'
import { getResourceCategories } from '@/services/resource-category'
import { Form } from 'element-ui'
import { dateFormat } from '@/utils/date'
import ResorceForm from './form.vue'

export default Vue.extend({
  name: 'ResourceList',
  components: { ResorceForm },
  data () {
    return {
      resources: [], // 资源列表
      form: {
        name: '',
        url: '',
        current: 1, // 默认查询第1页数据
        size: 15, // 每页大小
        categoryId: null // 资源分类
      },
      totalCount: 0,
      resourceCategories: [], // 资源分类列表
      isLoading: true, // 加载状态
      showDialog: false,
      isEdit: false,
      current: {} as FromServer.ResourceItem
    }
  },

  created () {
    this.loadResources()
    this.loadResourceCategories()
  },

  methods: {
    async loadResourceCategories () {
      const { data } = await getResourceCategories()
      this.resourceCategories = data.data
    },

    async loadResources () {
      this.isLoading = true
      const { data } = await getResourcePages(this.form)
      this.resources = data.data.records.map((item: FromServer.ResourceItem) => {
        return {
          ...item,
          createdTime: dateFormat(item.createdTime)
        }
      })
      this.totalCount = data.data.total
      this.isLoading = false
    },

    onSubmit () {
      this.form.current = 1
      this.loadResources()
    },

    handleEdit (item: FromServer.ResourceItem) {
      this.isEdit = true
      this.current = item
      this.showDialog = true
    },

    handleDelete (item: FromServer.ResourceItem) {
      this.$confirm('是否要删除该资源?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await deleteResource(item.id)
          this.loadResources()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    onFormSubmit () {
      this.showDialog = false
      this.loadResources()
    },

    adResource () {
      this.isEdit = false
      this.showDialog = true
    },

    handleSizeChange (val: number) {
      this.form.size = val
      this.form.current = 1
      this.loadResources()
    },

    handleCurrentChange (val: number) {
      this.form.current = val
      this.loadResources()
    },

    onReset () {
      (this.$refs.form as Form).resetFields()
      this.form.current = 1
      this.loadResources()
    }
  }
})
</script>

<style lang="scss" scoped>
.filter-container {
  margin-top: 15px;
  display: flex;
  justify-content: space-between;
}

.resource-body {
  .el-table {
    background-color: #fff;
  }
}
</style>
